/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package graphMaker;

import aliasMatching.controller.TimeEquality;
import aliasMatching.model.Alias;
import java.awt.Dimension;
import java.io.FileOutputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.swing.JPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 *
 * @author ITE
 */
public class makeGraph {

    List firstUserPostTime;
    List secondUserPostTime;
    String firstUser, secondUser;

    public JPanel createTimegraphPanel(int clickedUser) {
        try {
            FileOutputStream fos = new FileOutputStream("C:/image.PNG"); 
            dividePost(clickedUser);
            JFreeChart jfreechart = createTimeChart(createTimeDataset(clickedUser));
            ChartUtilities.writeScaledChartAsPNG(fos, jfreechart, 700, 260, 5, 5);
            ChartPanel chartpanel = new ChartPanel(jfreechart);
            chartpanel.setPreferredSize(new Dimension(700, 260));
            return chartpanel;
        } catch (Exception eee) {
            return null;
        }
    }

    private JFreeChart createTimeChart(CategoryDataset xydataset) {
        JFreeChart jfreechart = ChartFactory.createBarChart("User Posts", "Hour", "Posts", xydataset,
                PlotOrientation.VERTICAL, true, true, false);
        return jfreechart;
    }

    private CategoryDataset createTimeDataset(int clickedUser) throws SQLException {
        DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
        HashMap<String, double[]> map = new HashMap<String, double[]>();

        
        //String mainUser = String.valueOf(clickedUser);
        List mainUserPost = Alias.getAllPostTime(clickedUser);
        int postSize = mainUserPost.size();
        
        //double[] mainUserVector = Alias.getTimeVectorArray(mainUserPost);
        //map.put(mainUser, mainUserVector);
        double[] firstUserVector = Alias.getTimeVectorArray(firstUserPostTime);
        map.put(firstUser, firstUserVector);
        double[] secondUserVector = Alias.getTimeVectorArray(secondUserPostTime);
        map.put(secondUser, secondUserVector);

        Iterator<String> kitr = map.keySet().iterator();
        while (kitr.hasNext()) {
            String k = kitr.next();
            double[] post_count = map.get(k);
            int totalPostCount = post_count.length;
            //int postSize = 0;

//            for (double i : post_count) {
//                postSize += i;
//            }

            for (Integer i = 0; i < totalPostCount; ++i) {
                double value = (double) post_count[i] * 0.1 / postSize;
                defaultcategorydataset.addValue(value, k, i);
            }
        }
        return defaultcategorydataset;
    }

    public void dividePost(int user) {
        firstUser = user + "_A";
        secondUser = user + "_B";
        firstUserPostTime = new ArrayList();
        secondUserPostTime = new ArrayList();
        List post = TimeEquality.getPostTime(user);

        for (int i = 0; i < post.size(); i++) {
            String date = post.get(i).toString();
            int day = Integer.parseInt(date.substring(8, 10));

            if (day % 2 != 0) {
                firstUserPostTime.add(post.get(i));
            } else {
                secondUserPostTime.add(post.get(i));
            }
        }
    }
}
